package com.baseweb.webadmin.controller.app;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.pojo.ByUserBizProduct;
import com.baseweb.webadmin.core.util.RequestPage;
import com.baseweb.webadmin.core.util.ResultPage;
import com.baseweb.webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zx on 2017/8/30.
 */
@Controller
@RequestMapping(value = "/appApiProduct")
public class ProductAppController  extends BaseController {

    @Autowired
    private UserService userService;

    /**查询我的报价信息列表
     *
     * @param requestPage
     * @return
     */
    @RequestMapping(value = "/getMyProductList")
    @ResponseBody
    public Map<String, Object> getMyProductList(@RequestParam(value = "userId", required = true) Integer userId,
                                                  @RequestParam(value = "status", defaultValue = "0") Integer status,
                                                  RequestPage requestPage) {
        requestPage = requestPage == null ? new RequestPage() : requestPage;
        Integer productType = 0;
        Integer productStatus = 0;
        int total = 0;
        if(status == 1){//待审核
            productType = -1;
        }else if(status == 2){//已审核
            productType = 3;
        }else if(status == 4){//未上架
            productStatus = -1;
        }else if(status == 3){//已上架
            productStatus = 1;
        }
        List<ByUserBizProduct> products = userService.selectProductListByPage(userId, productType, null, requestPage.getStart(),
                requestPage.getPageSize(), null, null, null, null, productStatus);
        total = userService.selectProductListCount(userId, productType, null, null, null, null, null, productStatus);
        ResultPage productPage = new ResultPage<>(total, requestPage.getPageSize(), requestPage.getCurPage(), products);
        return success(productPage,"查询成功");
    }

    /**查询我的报价信息列表
     *
     * @return
     */
    @RequestMapping(value = "/getBizProductList")
    @ResponseBody
    public Map<String, Object> getBizProductList(@RequestParam(value = "userId", required = true) Integer userId) {
        RequestPage requestPage = new RequestPage();
        requestPage.setPageSize(10000);
        Integer productType = 1;
        Integer productStatus = 1;
        List<ByUserBizProduct> products = userService.selectProductListByPage(userId, productType, null, requestPage.getStart(),
                requestPage.getPageSize(), null, null, null, null, productStatus);
        return success(products,"查询成功");
    }

    /**
     * 根据商品ID删除商品信息
     * @param id 商品ID
     * @param status 商品状态   1 - 上架；2 - 删除；-1 - 下架
     * @return 是否成功
     */
    @RequestMapping(value = "/updateProductStatus")
    @ResponseBody
    public Map<String, Object> updateProductStatus(@RequestParam(value = "id",required = true) Integer id,
                                      @RequestParam(value = "status",required = true) Integer status) {
        try {
            ByUserBizProduct byUserBizProduct = userService.findBizProduct(id);
            byUserBizProduct.setStatus(status);


            userService.insertBizProduct(byUserBizProduct);

            return success(byUserBizProduct,"状态更改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return fail("状态更改失败");
        }

    }
}
